package com.iwuyc.tools.commons.util.io;

import com.iwuyc.tools.commons.util.string.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class IOUtils {

    /**
     * 读取输入流
     *
     * @param inputStream 输入流
     * @return byte[]
     * @throws IOException 读取异常
     */
    public static String readAllToHex(InputStream inputStream) throws IOException {
        final byte[] bytes = readAll(inputStream);
        return StringUtils.byteToHexString(bytes);
    }

    /**
     * 读取输入流
     *
     * @param inputStream 输入流
     * @return byte[]
     * @throws IOException 读取异常
     */
    public static byte[] readAll(InputStream inputStream) throws IOException {
        final int available = inputStream.available();
        final byte[] bytes = new byte[available];
        final int length = inputStream.read(bytes, 0, available);
        if (length != available) {
            log.warn("Read length is not equal to available size.");
        }
        return bytes;
    }
}
